$(function() {
    init_vue();
});

function init_vue() {
    var vue = new Vue({
        el : "#myapp",
        data : {
            level:level,
            startRow : 0,
            endRow : 0,
            total : 0,
            firstPage : 0,
            lastPage : 0,
            pageNum : 1,
            pageSize : 15,
            list : null,
            setting : null,
            config : null,
            navigatepageNums : null,
            sum_balance:0
        },
        events : {
            'openOrganizationrCommissionSetWin':function (t) {
                var organization = new Object();
                organization.id = t.organization_id;
                organization.name = t.organization_name;
                this.$broadcast('openOrganizationrCommissionSetWin', {title:'设置返佣', organization:organization});
            },
            'selectOrg':function (t) {
                this.$broadcast('selectOrg', t);
            },
            'refreshCustomerList':function () {
                this.pageNum = 1;
                this.getData();
            },
            'refreshCustomerInfo': function (t) {
                for (var i=0; i<this.list.length; i++) {
                    if (this.list[i].id == t.customer.customer_id) {
                        this.list[i].name = t.customer.name;
                        this.list[i].real_name = t.customer.real_name;
                        this.list[i].mobile_phone = t.customer.mobile_phone;
                    }
                }
            },
            'refreshCustomerField': function (t) {
                for (var i=0; i<this.list.length; i++) {
                    if (this.list[i].id == t.customer_id) {
                        this.list[i][t.key] = t.value;
                    }
                }
            },
            'openTreeOrganizationWin': function (t) {
                this.$broadcast('openTreeOrganizationWin', {title:"选择机构", level:t.level, organization_id:t.organization_id});
            }
        },
        methods : {
            init : function() {
                close_layuiLayerLoad();
                this.getData();
            },
            showPage : function(page) {
                // alert(page);
                this.pageNum = page;
                this.getData();
            },
            getData : function() {
                showLoad();
                var t = this;
                postMsg(root+"/customer/tolist", {
                    page_num : t.pageNum,
                    page_size : t.pageSize,
                    level:t.level
                }, function(i) {
                    t.config = i.config;
                    t.list = null;
                    t.list = i.data.list;
                    t.setting = i.data.setting;
                    t.startRow = i.data.startRow;
                    t.endRow = i.data.endRow;
                    t.total = i.data.total;
                    t.firstPage = i.data.firstPage;
                    t.lastPage = i.data.lastPage;
                    t.navigatepageNums = i.data.navigatepageNums;
                    t.sum_balance = i.sum_balance;
                    hideLoad();
                });
            },
            openCustomerWin : function() {
                var t = this;
                var title = '添加帐号';
                this.$broadcast('openCustomerWin', {title:title, customer:{id:0, real_name:"", mobile_phone:"", password:"", level:t.level}});
            },
            openEditCustomerWin : function (id) {
                var t = this;
                var index = layer.load(0, {shade: [0.3, '#000000']}); //0代表加载的风格，支持0-2
                postMsg(root + "/customer/getCustomerInfo", {id: id}, function (i) {
                    layer.close(index);
                    var title = '修改帐号';
                    t.$broadcast('openCustomerWin', {title: title, customer: i});
                });
            },
            changeState : function(id, state) {
                var t = this;
                showLoad();
                postMsg(root+"/customer/changeState", {id:id, state:state}, function(i) {
                    if (i == 1) {
                        for (var i=0; i<t.list.length; i++) {
                            if (t.list[i].id == id) {
                                t.list[i].state = state;
                            }
                        }
                        if (state == 2) {
                            $.scojs_message('账号已停用', $.scojs_message.TYPE_ERROR);
                        } else if (state == 1) {
                            $.scojs_message('账号已启用', $.scojs_message.TYPE_OK);
                        }
                    }
                    hideLoad();
                });
            },
            reset_pass: function (id) {
                var t = this;
                showLoad();
                postMsg(root+"/customer/resetPass", {id:id}, function(i) {
                    if (i.code == 1) {
                        $.scojs_message(i.msg, $.scojs_message.TYPE_OK);
                    }
                    hideLoad();
                });
            },
            openOrganizationrCommissionSetWin : function (customer) {
                var organization = new Object();
                organization.id = customer.organization_id;
                organization.name = customer.organization_name;
                this.$broadcast('openOrganizationrCommissionSetWin', {title:'设置返佣', organization:organization});
            },
            openRiskControlRateWin : function (customer) {
                var organization = new Object();
                organization.customer_id = customer.id;
                organization.id = customer.organization_id;
                organization.risk_control_rate = customer.risk_control_rate;
                this.$broadcast('openRiskControlRateWin', {title:customer.organization_name+'--风险控制率设置', organization:organization});
            },
            gotoInfoPage : function (id) {
                // window.location.href = root + '#user/info/id/' + id;
            }
        }
    });
    vue.init();
}